﻿using System;
using System.Collections;
using UnityEngine;

/// <summary>
///     参数列表：(callback, interval, repeat, delay)
///     callback： 回调
///     interval: 触发间隔(秒)
///     repeat: 重复次数
///     delay: 延迟时间(秒)
/// </summary>
public class DelayInvoke : MonoBehaviour
{
    public static IEnumerator DelayInvokeDo(Action callBack, float delay)
    {
        yield return new WaitForSeconds(delay);
        callBack();
    }

    public static IEnumerator DelayInvokeRepeat(Action callBack, float interval, float repeat, float delay = -1)
    {
        if (delay >= 0) yield return new WaitForSeconds(delay);
        for (var i = 0; i < repeat; i++)
        {
            callBack();
            yield return new WaitForSeconds(interval);
        }
    }

    public static IEnumerator DelayInvokeRepeatForever(Action callBack, float interval, float delay = -1)
    {
        if (delay >= 0) yield return new WaitForSeconds(delay);
        while (true)
        {
            callBack();
            yield return new WaitForSeconds(interval);
        }
    }
}